/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multithread;

import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author NDA
 */
public class CongNhan implements Runnable {

    private Boolean check, temp;
    private Boolean done = true;

    public Boolean getDone() {
        return done;
    }
    private int tenCongNhan;
    private java.util.Random random = new java.util.Random();
    private Thread t;
    private BlockingQueue<String> kho;

    public CongNhan() {
        tenCongNhan = random.nextInt(100);
        t = new Thread(this, "Cong Nhan " + tenCongNhan);
        t.start();
        check = true;
        temp = false;
    }

    public synchronized void resume() {
        temp = true;
        check = false;
        this.notify();
    }

    public void pause() {
        check = true;
        temp = false;
    }

    @Override
    public synchronized void run() {
        while (true) {
            if (check) {
                try {
                    this.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(CongNhan.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (temp) {
                done = false;
                System.out.println("Công nhân " + tenCongNhan + ": Ok, em đi ngay!");
                try {
                    System.out.println("Công nhân " + tenCongNhan + ": Ok, đi thôi!");
                    int temp = random.nextInt(2);
                    Thread.sleep(temp * 1000);
                    done = true;
                    System.out.println("Công nhân " + tenCongNhan + ": "
                            + "Đã chuyển hàng về nhà máy, quay về hàng đợi đến lượt!");
                    System.out.println("----------------------------------------------------");
                    this.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(CongNhan.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}